package com.bookstore.spring.action;



import com.bookstore.spring.dao.BookMap;
import com.bookstore.spring.pojo.Book;
import com.bookstore.spring.pojo.ShoppingCart;
import com.bookstore.spring.service.BookService;
import com.bookstore.spring.util.FormUtils;
import com.bookstore.spring.web.BookStoreWebUtils;
import com.bookstore.spring.web.CriteriaBook;
import com.bookstore.spring.web.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by Admin on 2016/8/15.
 */
public class BookAction {

    private BookService bookService ;
    public BookService getBookService() {
        return bookService;
    }

    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    /**
     * 添加图书到数据库
     *
     * @param request
     * @param response
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     * @throws IOException
     * @throws ServletException
     */
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response)
            throws InvocationTargetException, NoSuchMethodException, InstantiationException,
            IllegalAccessException, NoSuchFieldException, IOException, ServletException {

        Book book = (Book) FormUtils.parseForm(request, Book.class, "UTF-8");
        System.out.println(book);
//        BookDao bookDao = new BookDaoImp();
        BookMap bookDao = new BookMap();
        PrintWriter out = response.getWriter();
        if (book == null) {
            out.print(false);
            ModelAndView mv = new ModelAndView("/addBook", ModelAndView.FORWARD);
            return mv;
        } else {
            out.print(true);
            bookDao.save(book);
            ModelAndView mv = new ModelAndView(request.getContextPath() + "/success", ModelAndView.REDIRECT);
            return mv;
        }

//        if (book.getTitle().trim().equals("") || book.getAuthor().trim().equals("")) {
//            String str = "请输入图书的完整信息!";
//            ModelAndView mv = new ModelAndView("/addBook", ModelAndView.FORWARD);
//            mv.addData("message", str);
//            return mv;
//        } else {
//            bookDao.save(book);
//            ModelAndView mv = new ModelAndView(request.getContextPath() + "/success", ModelAndView.REDIRECT);
//            return mv;
//        }
    }

    /**
     * 获取页面列表
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView("/WEB-INF/page/books", ModelAndView.FORWARD);

        String pageNoStr = request.getParameter("pageNo");
        String minPriceStr = request.getParameter("minPrice");
        String maxPriceStr = request.getParameter("maxPrice");
        int pageNo = 1;
        int minPrice = 0;
        int maxPrice = Integer.MAX_VALUE;
        try {
            pageNo = Integer.parseInt(pageNoStr);
        } catch (NumberFormatException e) {
        }
        try {
            minPrice = Integer.parseInt(minPriceStr);
        } catch (NumberFormatException e) {
        }
        try {
            maxPrice = Integer.parseInt(maxPriceStr);
        } catch (NumberFormatException e) {
        }
        //传入条件,根据传入的条件,获得criteriaBook 对象
        CriteriaBook criteriaBook = new CriteriaBook(minPrice, maxPrice, pageNo);
        //有对象就得有页面,通过getPage() 方法获得页面
        /*
           *之所以要写一个service ,就是为了放便调用,在service中封装了很多如下方法
           *BookMap bookDao=new BookMapper();
           *Page<Book> page= bookDao.getPage(criteriaBook);
         */
        Page<Book> page = bookService.getPage(criteriaBook);
//        request.setAttribute("bookPage", page);
//        request.getRequestDispatcher("/WEB-INF/page/books.jsp").forward(request, response);
        mv.addData("bookPage", page);
        return mv;
    }


    /**
     * 查看图书的详细信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String strId = request.getParameter("id");
        int id = -1;
        Book book = null;
        try {
            id = Integer.parseInt(strId);
        } catch (NumberFormatException e) {
        }
        if (id > 0) {
            book = bookService.getBook(id);
        }
        if (book == null) {
            ModelAndView mv = new ModelAndView(request.getContextPath() + "/error-1", ModelAndView.REDIRECT);
            return mv;
        }
        ModelAndView mv = new ModelAndView("/WEB-INF/page/book", ModelAndView.FORWARD);
        mv.addData("book", book);
        return mv;
    }

    /**
     * 添加到购物车中
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public ModelAndView addToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取商品id
        String idStr = request.getParameter("id");
        int id = -1;
        boolean flag = false;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
        }
        if (id > 0) {
            //获取购物车对象
            ShoppingCart cart = BookStoreWebUtils.getShoppingCart(request);
            //调用BookService 的 addToCart 方法把商品放入到购物车中
            flag = bookService.addToCart(id, cart);
        }
        if (!flag) {
            return new ModelAndView("/error", ModelAndView.REDIRECT);
        } else {
            return list(request, response);
        }
    }

    public void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String title = request.getParameter("title");
        long count=bookService.getCountWithName(title);
        PrintWriter out = response.getWriter();
        if (count > 0) {
            out.print(false);
        } else {
            out.print(true);
        }
    }


}
